package com.example;

import android.telephony.SmsMessage;

import org.json.JSONObject;

public class SmsMessageUtils {

    public static final String DISPLAYMESSAGEBODY = "displayMessageBody";

    public static final String ORIGINATINGADDRESS = "originatingAddress";

    public static final String TIMESTAMPMILLIS = "timestampMillis";

    public static final String DISPLAYORIGINATINGADDRESS = "displayOriginatingAddress";

    /******
     *
     * 把信息封装成json的样式。
     * @param smsMessage
     * @return
     */
    public static String toDto(SmsMessage smsMessage) {
        JSONObject object = null;
        try {
            object = new JSONObject();
            object.accumulate(DISPLAYMESSAGEBODY, smsMessage.getMessageBody());
            object.accumulate(ORIGINATINGADDRESS, smsMessage.getOriginatingAddress());
            object.accumulate(TIMESTAMPMILLIS, smsMessage.getTimestampMillis());
            object.accumulate(DISPLAYORIGINATINGADDRESS, smsMessage.getDisplayOriginatingAddress());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object.toString();
    }

    /*****
     *
     * 数据库查询
     * @param displayMessageBody
     * @param timestampMillis
     * @param originatingAddress
     * @return
     */
    public static String toDto(String displayMessageBody, long timestampMillis, String originatingAddress) {
        JSONObject object = null;
        try {
            object = new JSONObject();
            object.accumulate(DISPLAYMESSAGEBODY, displayMessageBody);
            object.accumulate(ORIGINATINGADDRESS, originatingAddress);
            object.accumulate(TIMESTAMPMILLIS, timestampMillis);
            object.accumulate(DISPLAYORIGINATINGADDRESS, displayMessageBody);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object.toString();
    }


    public static String toDtoV2(String phone, String context) {
        JSONObject object = null;
        try {
            object = new JSONObject();
            object.accumulate(DISPLAYMESSAGEBODY, context);
            object.accumulate(ORIGINATINGADDRESS, phone);
            object.accumulate(TIMESTAMPMILLIS, System.currentTimeMillis());
            object.accumulate(DISPLAYORIGINATINGADDRESS, phone);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object.toString();
    }
}

